package edu.unh.sustain;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class WestEdgeExpressActivity extends Activity {
	
	String timeframe;
	Context ct = this;
	int tabNum = 1;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    
	    timeframe = getTimeframe(savedInstanceState);
	    ArrayAdapter<CharSequence> adapter;
	    
	    if( timeframe.equals("weekends"))
	    {
	    	setContentView(R.layout.weekend);
	    	adapter = ArrayAdapter.createFromResource(
		            this, R.array.timeframesdown, android.R.layout.simple_spinner_item);
	    	
	    	String[] stops = getResources().getStringArray(R.array.weekendstops);
	    	String[] req = getResources().getStringArray(R.array.weekendreq);
	    	TableLayout tbl = (TableLayout)findViewById(R.id.table1);
	    	
	    	for(int i = 0; i < stops.length; i++)
	    	{
	    		tbl.setColumnStretchable(i, true);
	    		TableRow tr = new TableRow(tbl.getContext());
	    		TextView tv = new TextView(this);
	    		TextView tvreq = new TextView(this);
	    		tv.setTextSize(12);
	    		tvreq.setTextSize(12);
	    		tv.setText(stops[i]);
	    		tvreq.setText(req[i]);
	    		tr.addView(tv);
	    		tr.addView(tvreq);
	    		tbl.addView(tr);
	    	}
	    }
	    else
	    {
	    	setContentView(R.layout.westedge);
	    	adapter = ArrayAdapter.createFromResource(
		            this, R.array.timeframesup, android.R.layout.simple_spinner_item);
	    	
	    	String[] stops = getResources().getStringArray(R.array.westedgestops);
	    	String[] req = getResources().getStringArray(R.array.westedgereq);
	    	TableLayout tbl = (TableLayout)findViewById(R.id.table1);
	    	
	    	for(int i = 0; i < stops.length; i++)
	    	{
	    		TableRow tr = new TableRow(tbl.getContext());
	    		TextView tv = new TextView(this);
	    		TextView tvreq = new TextView(this);
	    		tv.setTextSize( 12 );
	    		tvreq.setTextSize( 12 );
	    		tv.setText(stops[i]);
	    		tvreq.setText(req[i]);
	    		tr.addView(tv);
	    		tr.addView(tvreq);
	    		tbl.addView(tr);
	    	}
	    }
	    
	    Spinner spinner = (Spinner) findViewById(R.id.timeframes);
	    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    spinner.setAdapter(adapter);
	    spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
	}
	
	/**
	 * Extracts the timeframe data from the Instance and returns it
	 * 
	 * @param savedInstanceState previous state
	 * @return the string or null
	 */
	private String getTimeframe(Bundle savedInstanceState)
	{
		String ret = null;
		
		if( savedInstanceState != null )
	    {
	    	ret = (String) savedInstanceState.getString("timeframe");
	    }
	    else
	    {
	    	Bundle extras = getIntent().getExtras();
	    	
	    	if( extras != null )
	    		ret = extras.getString("timeframe");
	    }
		
		return ret;
	}
	
	private String getOpposite(String current)
	{
		if(current.equals("weekdays"))
			return new String("weekends");
		else
			return new String("weekdays");
	}
	
	public class MyOnItemSelectedListener implements OnItemSelectedListener {		
	    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
	    	Intent i;
	    	
	    	if(pos == 1)
	    	{
	    		i = new Intent();
	    		i.setClass( ct, CampusConnectorActivity.class);
		    	i.putExtra("timeframe", getOpposite(timeframe));
		    	i.putExtra("tab", tabNum);
                startActivityForResult(i, 0);
	    	}
	    	
	    }

	    public void onNothingSelected(AdapterView<?> parent) {
	    	// Do nothing.
	    }
	}
	
	/**
	 * Saves the state of the screen.  This is called on orientation changes. 
	 */
	@Override
	public void onSaveInstanceState(Bundle savedInstanceState)
	{
		  savedInstanceState.putString("timeframe", timeframe);
		  super.onSaveInstanceState(savedInstanceState);
	}
}
